package com.javacoo.event.client.starter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.javacoo.event.client.api.EventHelper;
import com.javacoo.event.client.config.EventConfig;
import com.javacoo.xkernel.spi.ExtensionLoader;

import lombok.extern.slf4j.Slf4j;

/**
 * 自动配置类
 * <li></li>
 * @author duanyong
 * @date 2021/10/15 22:50
 */
@Slf4j
@Configuration                                                                                                                              
@EnableConfigurationProperties(value = EventConfig.class)
@ConditionalOnClass(EventConfig.class)
@ConditionalOnProperty(prefix = EventConfig.PREFIX, value = EventConfig.ENABLED, matchIfMissing = true)
public class EventAutoConfiguration {
	@Autowired
    private EventConfig eventConfig;
	@Bean
	@ConditionalOnMissingBean(EventHelper.class)
	public EventHelper createEventHelper() {
        log.info("初始化事件处理器,实现类名称:{}",eventConfig.getImpl());
        EventHolder.eventHelper = ExtensionLoader.getExtensionLoader(EventHelper.class).getExtension(eventConfig.getImpl());
        log.info("初始化事件处理器成功,实现类:{}",EventHolder.eventHelper);
       return EventHolder.eventHelper;
	}
}
